﻿using org.critterai.nav;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class NavMeshViewer : MonoBehaviour
{
    public NavManagerProvider provider;
    public Transform startTransform;
    public Transform endTransform;
    public Transform targetTransform;
    public float radius;

    private MeshRenderer mMeshRenderer;
    private MeshFilter mMeshFilter;
    private Mesh mMesh;
    private CAINavmeshController mCAINavmeshController = new CAINavmeshController();
    private List<Vector3> mPathPointList = new List<Vector3>();
    private List<Vector3> mUnityPathPointList = new List<Vector3>();

    void Awake()
    {
        mMeshRenderer = GetComponent<MeshRenderer>();
        mMeshFilter = GetComponent<MeshFilter>();

        if (mMeshRenderer == null)
        {
            gameObject.AddComponent<MeshRenderer>();
        }

        if (mMeshFilter == null)
        {
            mMeshFilter = gameObject.AddComponent<MeshFilter>();
        }
    }

    void Start()
    {
        mCAINavmeshController.SetNavManagerProvider(provider);
        mCAINavmeshController.StartNavMesh();
    }

    void Update()
    {
        FindPathTest();
        //CalculatePositionHeightTest();
        //CalculatePositionIsWalkable();
        //RaycastTest();
        //MoveAlongSurfaceTest();
    }

    public void OnDrawGizmos()
    {
        Gizmos.color = Color.red;
        DrawGizmosLine(mPathPointList);

        Gizmos.color = Color.green;
        DrawGizmosLine(mUnityPathPointList);
        Gizmos.color = Color.white;
    }

    private void DrawGizmosLine(List<Vector3> linePointList)
    {
        int lineCount = linePointList.Count - 1;
        for (int i = 0; i < lineCount; ++i)
        {
            Gizmos.DrawLine(linePointList[i], linePointList[i + 1]);
        }
    }

    public void FindPathTest()
    {
        for (int i = 0; i < 1; ++i)
        {
            mPathPointList = mCAINavmeshController.FindPath(startTransform.position, endTransform.position);
        }
    }

    public void CalculatePositionHeightTest()
    {
        float height = 0.0f;
        if (mCAINavmeshController.GetPolyHeight(targetTransform.position, out height))
        {
            Debug.Log(height);
        }
    }

    public void CalculatePositionIsWalkable()
    {
        bool result = mCAINavmeshController.IsPositionWalkable(targetTransform.position);

        Debug.Log(result);
    }

    public void RaycastTest()
    {
        mPathPointList.Clear();

        if (mCAINavmeshController.Raycast(startTransform.position, endTransform.position))
        {
            mPathPointList.Add(startTransform.position);
            mPathPointList.Add(endTransform.position);
        }
    }

    public void MoveAlongSurfaceTest()
    {
        mPathPointList.Clear();

        Vector3 resultPoint = Vector3.zero;
        if (mCAINavmeshController.MoveAlongSurface(startTransform.position, endTransform.position, out resultPoint))
        {
            mPathPointList.Add(startTransform.position);
            mPathPointList.Add(resultPoint);
        }
    }
}
